import 'package:xiaomi_shop/app/services/storage.dart';

class SearchService {
  // 添加搜索历史
  static addSearchHistory(String keyword) async {
    List? tempList = await Storage.getData("searchHistory");
    if (tempList != null) {
      print('key $keyword');
      bool hasData = tempList.any((element) => keyword == element);
      if (!hasData) {
        tempList.add(keyword);
        await Storage.setData("searchHistory", tempList);
      }
    } else {
      print('key1 $keyword');
      tempList = [];
      tempList.add(keyword);
      await Storage.setData("searchHistory", tempList);
    }
  }

  // 获取搜索历史
  static Future<List?> getSearchHistory() async {
    List? tempList = await Storage.getData("searchHistory");
    if (tempList != null) {
      return tempList;
    } else {
      return [];
    }
  }

  // 删除搜索历史
  static deleteSearchHistory(String keyword) async {
    List? tempList = await Storage.getData("searchHistory");
    if (tempList!.contains(keyword)) {
      tempList.remove(keyword);
      await Storage.setData("searchList", tempList);
    }
  }

  // 清空搜索历史
  static cleanSearchHistory() async {
    await Storage.removeData("searchHistory");
  }
}
